package com.seven.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.validation.constraints.Negative;
import javax.validation.constraints.Positive;

/**
 * @Author seven
 * @Date 2020/4/24 14:23
 **/
@Configuration
@EnableConfigurationProperties(MyEncodingProperties.class)
public class MyEncodingConfig {
    private final MyEncodingProperties properties;

    public MyEncodingConfig(MyEncodingProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean(CharacterEncodingFilter.class)
    public CharacterEncodingFilter characterEncodingFilter() {




                CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(MyEncodingProperties.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(MyEncodingProperties.Type.RESPONSE));
        return filter;
    }
}
